package performance.jmeter;

import performance.BaseTpsTest;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 多客户端多线程测数据库性能测试 <br/>
 * 不同库同表
 *
 * @author wangwy
 *
 */
public class TpsInitDataExecutor extends BaseTpsTest {


    private static final String  STR_LONG_TEXT = "疯似的飞三番四复三番四复三番四复收发室圣克拉拉开发放到沙发上发撒旦发生发生发撒旦发撒发撒旦发撒发生飞洒";
    private static final AtomicInteger insertCount = new AtomicInteger(0);
    private static final AtomicInteger groupInsertCount = new AtomicInteger(0);

    public void doInitData(int insertNum,long startId) throws SQLException {
        int count = 0;
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into score(id,name,score,course) values(?,?,?,?)");
            conn.setAutoCommit(false);
            pStmt.setQueryTimeout(0);
            int r=0;
            while ((r=insertCount.getAndIncrement()) < insertNum) {
                count++;
                long temp = (long)(Math.random()*500000);
                long id = r+startId;
                System.out.println("id="+id);
                pStmt.setLong(1, id);
                pStmt.setString(2, "yc"+temp);
                pStmt.setInt(3, 60);
                pStmt.setString(4, temp+STR_LONG_TEXT);
                pStmt.addBatch();
                if(count%1000==0){
                    pStmt.executeBatch();
                }
            }
            pStmt.executeBatch();
            conn.commit();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

    /**
     * 准备更新语句的数据,表t_student
     * @param insertNum
     * @param startId
     * @throws SQLException
     */
    public void doInitUpdateData(int insertNum,long startId) throws SQLException {
        int count = 0;
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into t_student(id,name,age,detail) values(?,?,?,?)");
            conn.setAutoCommit(false);
            pStmt.setQueryTimeout(0);
            int r=0;
            while ((r=insertCount.getAndIncrement()) < insertNum) {
                count++;
                long temp = (long)(Math.random()*500000);
                long id = r+startId;
                System.out.println("id="+id);
                pStmt.setLong(1, id);
                pStmt.setString(2, "name"+temp);
                pStmt.setInt(3, 18);
                pStmt.setString(4, "detail"+temp);
                pStmt.addBatch();
                if(count%1000==0){
                    pStmt.executeBatch();
                }
            }
            pStmt.executeBatch();
            conn.commit();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

    public void doInitGroupByData(int insertNum,long startId) throws SQLException {
        int count = 0;
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into t_user(id,name,salary) values(?,?,?)");
            conn.setAutoCommit(false);
            int r;
            while ((r=groupInsertCount.getAndIncrement()) < insertNum) {
                count++;
                long temp = (long)(Math.random()*500000);
                long id = r+startId;
                System.out.println("id="+id+"==="+(id/1000));
                pStmt.setLong(1, id);
                pStmt.setString(2, "nb"+ (id/1000));
                pStmt.setLong(3, temp);
                pStmt.addBatch();
                if(count%1000==0){
                    pStmt.executeBatch();
                }
            }
            pStmt.executeBatch();
            conn.commit();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

    public void doInitJoinData(int insertNum,long startId) throws SQLException {
        int count = 0;
        Connection conn = null;
        PreparedStatement pStmt = null;
        PreparedStatement orderPStmt;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into t_custom(id,name) values(?,?)");
            orderPStmt = conn.prepareStatement("insert into t_order(id,custom_id,name,content) values (?,?,?,?)");
            conn.setAutoCommit(false);
            int r;
            while ((r=insertCount.getAndIncrement()) < insertNum) {
                count++;
                long temp = (long)(Math.random()*500000);
                long id = r+startId;
                System.out.println("id="+id+"===");
                pStmt.setLong(1, id);
                pStmt.setString(2, "yc"+temp);
                pStmt.addBatch();


                for(long i=r*5;i<(r+1)*5;i++){
                    System.out.println(id+"==="+(i+startId));
                    orderPStmt.setLong(1,i+startId);
                    orderPStmt.setLong(2,id);
                    orderPStmt.setString(3,"name"+temp);
                    orderPStmt.setString(4,temp+"content"+temp);
                    orderPStmt.addBatch();
                }

                if(count%1000==0){
                    pStmt.executeBatch();
                    orderPStmt.executeBatch();
                }

            }
            pStmt.executeBatch();
            orderPStmt.executeBatch();
            conn.commit();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

}
